# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# steam Steam实时在线人数
# https://store.steampowered.com/stats
# scrapy crawl steam_online
class SteamOnlineSpider(scrapy.Spider):
    name = 'steam_online'

    allowed_domains = ['store.steampowered.com']

    start_urls = ['http://store.steampowered.com/']

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.steam.SteamOnlinePipeline': 300
        }
    }

    def start_requests(self):
        urls = [
            'https://store.steampowered.com/stats'
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse, headers=util.default_headers)

    def parse(self, response):
        for index in range(3, 103):
            item_xpath = f'//*[@id="detailStats"]/table/tr[{index}]/'
            title = response.xpath(f'{item_xpath}td[4]/a/text()').get()
            online = response.xpath(f'{item_xpath}td[1]/span/text()').get().replace(",", '')
            online_max = response.xpath(f'{item_xpath}td[2]/span/text()').get().replace(",", '')
            link = response.xpath(f'{item_xpath}td[4]/a/@href').get()

            yield util.make_data({
                'source': 1,
                'no': index - 2,
                'name': title,
                'online': online,
                'online_max': online_max,
                'link': link,
            })

        pass
